
/*
 * This is the extension activity for a 'SilentAction' activity associated
 * with service '{http://www.pi4soa.org/AuctionHouseBuyer}AuctionHouse'
 * and behavior 'main_AuctionHouse'. The name of the
 * activity is 'CommitBidAuctionDone'.
 */
package org.pi4soa.auctionhousebuyer.auctionhouse.main_auctionhouse;

import org.pi4soa.service.extensions.DefaultActivityExtension;
import org.pi4soa.service.extensions.SilentActivityExtension;
import org.pi4soa.service.extensions.ExtensionContext;
import org.pi4soa.service.ServiceException;
import org.pi4soa.service.DeferProcessingException;

import dk.itu.amds.auctionhouse.data.AuctionHouseDB;
import dk.itu.amds.auctionhouse.domain.AuctionItem;
import dk.itu.amds.auctionhouse.domain.Bid;

/**
 * This class has been autogenerated to implement the custom
 * activity within a service endpoint description.
 */
public class CommitBidAuctionDoneActivity extends DefaultActivityExtension
					implements SilentActivityExtension {

	/**
	 * The constructor for the activity extension.
	 */
	public CommitBidAuctionDoneActivity() {
	}
	
	/**
	 * This method enables an activity extension to
	 * perform activities that are not observable.
	 * 
	 * @param context The context
	 * @exception DeferProcessingException Suspend processing
	 * @exception ServiceException Failed to perform silent action
	 */
	public void process(ExtensionContext context)
				throws DeferProcessingException, ServiceException {

		System.err.println("AUCTIONHOUSE commits bid.");

		AuctionHouseDB db = new AuctionHouseDB();
		
		int userId = Integer.parseInt(context.getVariable("userId").toString());
		float currentBid = Float.parseFloat(context.getVariable("currentBid").toString());
		int auctionItemId = Integer.parseInt(context.getVariable("itemId").toString());
		AuctionItem item = db.getAuctionHouse().getAuctionItems().get(auctionItemId);
		
		Bid bid = new Bid(userId, currentBid);

		// Add the bid to the current item.
		item.addBid(bid);
		item.setAuctionDone(true);
		db.store();
		// TODO: Add code to perform tasks relevant to
		// the Silent Action.
		
	}
}	